package com.spider.gp.processor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spider.gp.constant.UrlConstant;
import com.spider.gp.dao.GpTj;
import com.spider.gp.dao.GpTjDao;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * description: 昨日涨停
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class ZRZTProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private GpTjDao gpTjDao;


    @Override
    public void process(Page page) {

//        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
//        String reportTime =formatter.format(new Date());
//        String reportTime ="2022-06-13";
//        String s = page.getUrl().get();
//        String reportTime = s.substring(s.length() - 8);
//
//        try {
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//            Date date = formatter.parse(reportTime);
//            SimpleDateFormat formatter01 = new SimpleDateFormat("yyyy-MM-dd");
//            reportTime = formatter01.format(date);
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }


        List<String> all = page.getJson().jsonPath("$.data.klines").all(); //数据

        String curPriceJson = all.get(0);
        String[] split = curPriceJson.split(",");
        String reportTime = split[0];
        String rate = split[1];

        LambdaQueryWrapper<GpTj> wrapperQ = new LambdaQueryWrapper<>();
        wrapperQ.eq(GpTj::getReportTime, reportTime);
        GpTj gpTj = gpTjDao.selectOne(wrapperQ);

        if (gpTj == null) {
            gpTjDao.insert(GpTj.builder().reportTime(reportTime).build());
        }

//        List<String> all = page.getJson().jsonPath("$.data").all();
//        HashMap hashMap = JSON.parseObject(all.get(0), HashMap.class);
//        BigDecimal f170 = new BigDecimal(hashMap.get("f170").toString()).divide(new BigDecimal(100));
        StringBuffer str = new StringBuffer();


        if (page.getUrl().toString().contains(UrlConstant.DFCF_ZR_ZT_ZS_LS)) {

            if (gpTj != null && Strings.isNotBlank(gpTj.getZrztl())) {
                str.append(gpTj.getZrztl()).append(";");
            }
            str.append(rate).append("%");

            gpTjDao.updateById(GpTj.builder().reportTime(reportTime).zrztl(str.toString()).build());
        }

        if (page.getUrl().toString().contains(UrlConstant.DFCF_ZR_LB_ZS_LS)) {

            if (gpTj != null && Strings.isNotBlank(gpTj.getZrlbl())) {
                str.append(gpTj.getZrlbl()).append(";");
            }
            str.append(rate).append("%");

            gpTjDao.updateById(GpTj.builder().reportTime(reportTime).zrlbl(str.toString()).build());

        }


    }

    @Override
    public Site getSite() {
        return site;
    }
}








